package com.hrt.manage.sncode.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.ProductDetails;
import com.hrt.bean.ProductInfor;
import com.hrt.bean.ResourceType;
import com.hrt.bean.SnCodeSequence;
import com.hrt.bean.StockDetail;
import com.hrt.manage.sncode.dao.SNcodeDao;
import com.hrt.manage.sncode.service.SNCodeService;
import com.hrt.util.WebParams;

@Service
public class SNCodeServiceImpl implements SNCodeService {

	
	@Autowired
	private SNcodeDao dao;
	
	@Override
	public Long saveSnCode(SnCodeSequence snCode) {
		// TODO Auto-generated method stub
		return dao.saveEntity(snCode);
	}

	@Override
	public Long updateSnCode(SnCodeSequence snCode) {
		// TODO Auto-generated method stub
		 dao.updateEntity(snCode);
		return snCode.getId();
	}

	@Override
	public SnCodeSequence getSnCodeById(Long id) {
		// TODO Auto-geated method stub
		return dao.getById(id);
	}

	@Override
	public SnCodeSequence getSnCodeByCode(String snCode) {
		return dao.getSnCodeByCode(snCode);
	}
	@Override
	public SnCodeSequence getSnCodeByCode(String snCode,Integer reType) {
		return dao.getSnCodeByCode(snCode,reType);
	}


	@Override
	public List<SnCodeSequence> genarateSnCodeByCode(StockDetail detail,Integer resourceType) {
		// TODO Auto-generated method stub
		List<SnCodeSequence> snCodes=new ArrayList<SnCodeSequence>();
		Long total=detail.getTotal();
		if (resourceType == ResourceType.PRODUCT_RESOURCE) {
			for(int idx=0;idx<total;idx++)
			{
				
				String snCodeStr=genarateSnCode();
				if(snCodeStr!=null && !"".equals(snCodeStr)){
					
				SnCodeSequence snCode=new SnCodeSequence();
				snCode.setCreateTime(this.dao.getDate());
				snCode.setStatus(new Integer(0));
				snCode.setIsdelete(new Integer(0));
				snCode.setSnCode(snCodeStr);
				snCode.setStockId(detail.getStockId());
				snCode.setStkDeId(detail.getId());
				snCode.setReType(ResourceType.PRODUCT_RESOURCE);
				snCodes.add(snCode);
				
				}
				
			}
			
			
		}
		if (resourceType== ResourceType.GIFT_RESOURCE) {
			for(int idx=0;idx<total;idx++)
			{
				String snCodeStr=genarateSnCode();
				if(snCodeStr!=null && !"".equals(snCodeStr)){
					
				SnCodeSequence snCode=new SnCodeSequence();
				snCode.setCreateTime(this.dao.getDate());
				snCode.setStatus(new Integer(0));
				snCode.setIsdelete(new Integer(0));
				snCode.setSnCode(snCodeStr);
				snCode.setStockId(detail.getStockId());
				snCode.setStkDeId(detail.getId());
				snCode.setReType(ResourceType.GIFT_RESOURCE);
				snCodes.add(snCode);
				
				}
				
			}
			
		}
	
		return snCodes;
	}

	/**
	 * 生成条形码
	 */
	private String genarateSnCode() {
		// TODO Auto-generated method stub
		Long seqNum=WebParams.getSnCodeSeqNum();
		String seqNumStr=new String();
		if(seqNum<10)
		{
			seqNumStr="000"+seqNum;
		}
		else if(seqNum<100)
		{
			seqNumStr="00"+seqNum;
		}
		else if(seqNum<1000)
		{
			seqNumStr="0"+seqNum;
		}
		Date date=new Date();
		SimpleDateFormat mf=new SimpleDateFormat("yyyyMMdd");
		String snCode=mf.format(date);
		snCode=snCode+seqNumStr.toString();
		return snCode;
	}

	/**
	 * 
	 */
	@Override
	public List<SnCodeSequence> querySnCodeByStockId(Long stockId) {
		// TODO Auto-generated method stub
		 List<SnCodeSequence> seq=new ArrayList();
		 seq=this.dao.querySnCodeByStockId(stockId);
		return seq;
	}

	/**
	 * 删除条形码
	 */
	@Override
	@Transactional
	public Long delSNCodeById(Long id) {
		// TODO Auto-generated method stub
		SnCodeSequence snCode=dao.getById(id);
		if(snCode!=null){
		snCode.setIsdelete(new Integer(1));
		dao.updateEntity(snCode);
		return snCode.getId();
		}
		return null;	}

}
